<?php

namespace app\storeapi\controller\goods;

use app\storeapi\controller\Controller;
use app\storeapi\model\Category as CategoryModel;
use app\storeapi\validate\Category as CategoryValidate;

/**
 * 商品分类
 * Class Category
 * @package app\store\controller\goods
 */
class Category extends Controller
{

    /**
     * 商品分类列表
     * @return mixed
     */
    public function index()
    {
        $model = new CategoryModel;
        $data = $model->getCacheTree();
        return $this->renderSuccess($data);
    }

    /**
     * 添加商品分类
     * @return array|mixed
     */
    public function add()
    {
        //获取数据
        $param = $this->postData();
        $validate = new CategoryValidate;
        // 表单验证
        if (!$validate->scene('add')->check($param)) {
            return $this->renderError($validate->getError());
        }
        $model = new CategoryModel;
        $check = $model->checkAdd($param);
        if($check) return $this->renderError('分类名称不能重复');
        //商品添加
        $flag = $model->add($param);
        return $msg = $flag?$this->renderSuccess([],'添加成功'):$this->renderError('添加失败');
    }


    /**
     * 获取商品分类详情
     * @return array|mixed
     */
    public function detail()
    {
        //获取数据
        $param = $this->postData();
        $validate = new CategoryValidate;
        // 表单验证
        if (!$validate->scene('detail')->check($param)) {
            return $this->renderError($validate->getError());
        }
        $model = (new CategoryModel)::get($param['category_id'], ['image']);
        $list = $model->getCacheTree();
        return $this->renderSuccess(compact('model','list'));
    }



    /**
     * 编辑商品分类
     * @param $category_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $param = $this->postData();
        //模板详情
        $model = (new CategoryModel)::get($param['category_id'], ['image']);
        if(empty($model)){
            return $this->renderError('该分类不存在，请刷新后重试');
        }
        $validate = new CategoryValidate;
        // 表单验证
        if (!$validate->scene('edit')->check($param)) {
            return $this->renderError($validate->getError());
        }
        $check = $model->checkEdit($param);
        if($check) return $this->renderError('分类名称不能重复');
        // 商品编辑
        $flag = $model->edit($param);
        return $msg = $flag?$this->renderSuccess([],'编辑成功'):$this->renderError('编辑失败');

    }

    /**
     * 删除商品分类
     * @param $category_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        $validate = new CategoryValidate;
        // 表单验证
        if (!$validate->scene('del')->check($param)) {
            return $this->renderError($validate->getError());
        }
        $model = (new CategoryModel)::get($param['category_id']);
        if(empty($model)){
            return $this->renderError('该分类不存在，请刷新后重试');
        }
        if (!$model->remove($param['category_id'])) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

}
